package com.gjs.cd.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gjs.cd.pojo.entity.CdCodeGenerateBatch;
import com.gjs.cd.pojo.vo.BatchVo;
import com.gjs.cd.service.ICdCodeGenerateBatchService;
import com.gjs.common.manager.response.RspBody;
import com.gjs.common.pojo.dto.request.PageQuery;
import com.gjs.common.pojo.dto.response.ListPageRsp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhongwenguang
 * @date 2022-3-21 17:10
 */
@RestController
@Slf4j
@Api(tags = "批次管理")
@RequestMapping("/batch")
@RequiredArgsConstructor
public class CdCodeGenerateBatchController {
    private final ICdCodeGenerateBatchService iCdCodeGenerateBatchService;
    @Value("${cd.singleMaxNum}")
    private Integer singleMaxNum;
    //新增批次
    @PostMapping("/add")
    @ApiOperation("新增")
    public RspBody<Boolean> add(CdCodeGenerateBatch cdCodeGenerateBatch){
        int num = cdCodeGenerateBatch.getTargetNum();//目标数量
        if (num > singleMaxNum){
            return RspBody.error("单次生成激活码数量不等超过50万");
        }
        return RspBody.success(iCdCodeGenerateBatchService.addBatch(cdCodeGenerateBatch));
    }

    /**
     *
     * @param cdId 品种id
     * @param cdCodeValidTermId cdCodeValidTermId的id
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("分页列表")
    public RspBody<ListPageRsp<BatchVo>> list(PageQuery pageQuery, Long cdId, Long cdCodeValidTermId){
        IPage<BatchVo> batchVoIPage = iCdCodeGenerateBatchService.getBatchList(pageQuery,cdId,cdCodeValidTermId);
        return RspBody.success(ListPageRsp.of(batchVoIPage));
    }

}
